﻿Imports System.Data
Imports System.Data.SqlClient


Public Class DLocalidad
    Inherits conexion
    Dim cmd As New SqlCommand
    Public Function insertar(ByVal item As Entidades.ELocalidad) As Boolean
        Try
            conectado()
            cmd = New SqlCommand("insertar_localidad")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@nombre", item.nombreloc)
            cmd.Parameters.AddWithValue("@codpostal", item.codpostal)
            cmd.Parameters.AddWithValue("@provincia", item.provin)

            If cmd.ExecuteNonQuery Then
                Return True
            Else
                Return False
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        Finally
            desconectado()
        End Try
    End Function
    Public Function modificar(ByVal item As Entidades.ELocalidad) As Boolean
        Try
            conectado()
            cmd = New SqlCommand("modificar_localidad")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@codigo", item.cod_loc)
            cmd.Parameters.AddWithValue("@nombre", item.nombreloc)
            cmd.Parameters.AddWithValue("@codpostal", item.codpostal)
            cmd.Parameters.AddWithValue("@provincia", item.provin)

            If cmd.ExecuteNonQuery Then
                Return True
            Else
                Return False
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        Finally
            desconectado()
        End Try
    End Function
    Public Function eliminar(ByVal item As Entidades.ELocalidad) As Boolean
        Try
            conectado()
            cmd = New SqlCommand("eliminar_localidad")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@codigo", item.cod_loc)

            If cmd.ExecuteNonQuery Then
                Return True
            Else
                Return False
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        Finally
            desconectado()
        End Try
    End Function

    Public Function ver() As List(Of Entidades.ELocalidad)
        conectado()
        Dim listaElocalidad As New List(Of Entidades.ELocalidad)
        Dim objElocalidad As Entidades.ELocalidad
        cmd.Connection = cnn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "Select * from LOCALIDAD "

        Dim reader As SqlDataReader
        reader = cmd.ExecuteReader

        While reader.Read
            objElocalidad = New Entidades.ELocalidad
            objElocalidad.cod_loc = reader(0)
            objElocalidad.nombreloc = reader(1)
            objElocalidad.codpostal = reader(2)
            objElocalidad.provin = reader(3)
            listaElocalidad.Add(objElocalidad)
        End While
        Return listaElocalidad


        desconectado()


    End Function

    'Public Function buscarlocalidades(ByVal LOCALIDAD As String) As DataTable
    '    Try
    '        conectado()
    '        cmd = New SqlCommand("sp_buscarloc")
    '        cmd.CommandType = CommandType.StoredProcedure
    '        cmd.Connection = cnn
    '        cmd.Parameters.AddWithValue("@nombre", LOCALIDAD)
    '        Dim DA As New SqlDataAdapter(cmd)
    '        Dim TABLA As New DataTable
    '        TABLA.Clear()
    '        DA.Fill(TABLA)
    '        Return TABLA

    '        Return Nothing

    '    Catch ex As Exception
    '        MsgBox(ex.Message)
    '        Return Nothing
    '    Finally
    '        desconectado()
    '    End Try
    'End Function

   
End Class

